package com.example.allshengmedical.base;

import java.util.LinkedList;
import java.util.List;
import com.example.allshengmedical.R;
import com.example.allshengmedical.util.ByAlert;
import com.example.allshengmedical.util.CrashHandler;
import com.itboye.allshengmedical.sci.DefaultSerialControl;
import com.itboye.allshengmedical.sci.ISerialControl;

import android.app.Activity;
import android.app.Application;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;

/**
 * Created by WillJoe on 16/8/24.
 */
public class BaseApplication extends Application {
	// 分析am页面是否报错
	public static final String AnalyzeAmYESORNO = "analyzeamyesorno";
	// activityLayoutTableBeans（运行里）
	public static final String LayoutTableBeans = "activityLayoutTableBeans";
	// 运行的基因盘联动(暂时不用)
	public static final String RUNGENERECORD1 = "runGeneRecord1";
	public static final String RUNGENERECORD2 = "runGeneRecord2";
	// 存储最近20个实验的记录
	public static final String FILEMANGER_HISTORYARRAYLIST = "historyArrayList";

	// 准备实验之前文件管理中的选择的路径
	public static final String FILEMANGER_PREPAREFOLDERPATH = "preparedFolderPath";
	public static final String FILEMANGER_PREPAREFILENAME = "preparedFileName";

	// 基本信息中选择的实验类型
	public static final String BASEINFORMATION_EXPERIMENTTYPE = "experimentType";
	// 基本信息中选中的试剂
	public static final String BASEINFORMATION_REAGENT = "reagent";
	// 系统时间
	public static final String SYSTEM_LANGUAGE = "systemLanguage";
	// 自检结果
	public static final String SELF_CHECK = "selfcheck";
	// 亮度
	public static final String BRIGHTNESS = "BRIGHTNESS";

	public static Context context;
	// 端口
	public static ISerialControl controlInstance;

	public List<Activity> mList = new LinkedList<Activity>();
	public static BaseApplication instance;

	

	public BaseApplication() {
		super();
		// TODO Auto-generated constructor stub
	}

	public synchronized static BaseApplication getInstance() {
		if (null == instance) {
			instance = new BaseApplication();
		}
		return instance;
	}

	@Override
	public void onCreate() {
		super.onCreate();
		context = this;
//		CrashHandler.getInstance().init(this);
		controlInstance = DefaultSerialControl.getInstance();
		controlInstance.init();
		controlInstance.start();
		ISUDisk();
	}

	public static Context getContext() {
		return context;
	}

	// add Activity
	public void addActivity(Activity activity) {
		mList.add(activity);
	}

	// remove Activity
	public void removeActivity(Activity activity) {
		mList.remove(activity);
	}

	// finish all Activity
	public void exit() {
		try {
			for (Activity activity : mList) {
				if (activity != null)
					activity.finish();
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			System.exit(0);
		}
	}

	public void finishAll() {
		try {
			for (Activity activity : mList) {
				if (activity != null)
					activity.finish();
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {

		}
	}

	// Udisk
	public void ISUDisk() {
		IntentFilter iFilter = new IntentFilter();
		iFilter.addAction(Intent.ACTION_MEDIA_EJECT);
		iFilter.addAction(Intent.ACTION_MEDIA_MOUNTED);
		iFilter.addAction(Intent.ACTION_MEDIA_REMOVED);
		iFilter.addAction(Intent.ACTION_MEDIA_UNMOUNTED);
		iFilter.addDataScheme("file");
		USBBroadCastReceiver mBroadcastReceiver = new USBBroadCastReceiver();
		registerReceiver(mBroadcastReceiver, iFilter);

	}

	private class USBBroadCastReceiver extends BroadcastReceiver {
		@Override
		public void onReceive(Context context, Intent intent) {
			String action = intent.getAction();
			if (action.equals(Intent.ACTION_MEDIA_EJECT)) {
				// USB设备移除，更新UI
				ByAlert.alert(context.getString(R.string.baseapplication_u_in));
			} else if (action.equals(Intent.ACTION_MEDIA_MOUNTED)) {
				// USB设备挂载，更新UI
				ByAlert.alert(context.getString(R.string.baseapplication_u_out));

			}
		}
	}
}
